<template>
  <div>
    <Layout :showH="false" :showD="false" class="mybackgrund">
      <div class="container">
        <div class="header">
          <router-link to="/my"><van-icon name="arrow-left" /></router-link>
          <h2>交易记录</h2>
          <van-icon name="" />
        </div>
      </div>

      <!-- 选择事件 -->
      <div class="chose">
        <input type="text" v-model="time" readonly />
        <van-button type="primary" @click="show = true" size="small">选择时间</van-button>
      </div>
      <!-- <van-cell title="选择单个日期" :value="date" @click="show = true" /> -->
      <van-calendar v-model="show" @confirm="onConfirm" />
      <!-- 初始化时间 -->
      <div v-if="showA">
        <van-swipe-cell
          :before-close="beforeClose"
          v-for="it in list"
          :key="it.id"
        >
          <template #left>
            <van-button square type="primary" text="选择"/>
          </template>
          <van-cell :border="false" :title="'￥' + Number(it.cash).toFixed(2)" :value="it.time" />
          <template #right>
            <van-button square type="danger" text="删除" />
          </template>
        </van-swipe-cell>
      </div>
      <div v-if="showB">
        <!-- 筛选时间 -->
        <van-swipe-cell
          :before-close="beforeClose"
          v-for="it in filPerson"
          :key="it.id"
        >
          <van-cell :border="false" :title="'￥' + it.cash" :value="it.time" />
        </van-swipe-cell>
      </div>
    </Layout>
  </div>
</template>
<script>
import moment from "moment";
import { usersApi } from "@/apis/myApi";
import { editMoneyApi } from "@/apis/myApi";
export default {
  data() {
    return {
      list: [],
      userId: "",
      show: false,
      date: "",
      time: "",
      newTime: "",
      arr: [],
      filPerson: [],
      showA: true,
      projectTimer: null,
      showB: false,
    };
  },
  created() {
    this.out();
  },
  watch: {
    time(val) {
      this.filPerson = this.list.filter((p) => {
        return p.time.includes(val);
      });
      this.showA = false;
      this.showB = true;
    },
  },
  methods: {
    onConfirm(date) {
      this.show = false;
      // this.pageData.createTime = moment(date).format("yyyy-MM-DD");
      this.time = moment(date).format("yyyy-MM-DD");
      // console.log("-------------");
      // console.log(this.arr);
      // console.log(this.newTime);
    },
    async out() {
      this.userId = this.$store.getters["common/userId"];
      let result = await usersApi(this.userId);
      this.list = result.withdrawal.reverse();
    },
    beforeClose({ position, instance }) {
      switch (position) {
        case "left":
        case "cell":
        case "outside":
          instance.close();
          break;
        case "right":
          this.$dialog
            .confirm({
              message: "确定删除吗？",
            })
            .then(() => {
              instance.close();
            });
          break;
      }
    },
  },
};
</script>


<style lang="less">
.goods-card {
  margin: 0;
  // background-color: @white;
}
.chose{
  width: 80vw;
  margin: auto;
  margin-top: 30px;
  position: relative;
  input{
    width: 50vw;
  }
  .van-button{
    position: absolute;
    left: 210px;
    top: 0px;
  }
}
.delete-button {
  height: 100%;
}
.container {
  padding: 0 20px;
  .header {
    padding-top: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    h2 {
      font-size: 1rem;
      font-weight: normal;
      margin: 0;
    }
  }
}
</style>